import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DateUtils {

    public static String getTimeByFileName(String fileName, String format) {
        // 创建正则表达式匹配模式
        Pattern pattern = Pattern.compile("\\d+");
        // 创建 Matcher 对象，并将字符串与正则表达式匹配
        Matcher matcher = pattern.matcher(fileName);
        // 遍历匹配结果
        String timeStampStr = "";
        while (matcher.find()) {
            // 获取匹配到的数字部分
            timeStampStr += matcher.group();
        }
        String year = timeStampStr.substring(0, 3);
        try {
            Date date = null;
            if (Main.effectiveYear.contains(year)) {//是时间
                timeStampStr = timeStampStr.substring(0, 14);
                SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMddHHmmss");
                date = sdf1.parse(timeStampStr);
            } else {
                Long timeStamp = timeStampStr.length() == 14 ? Long.parseLong(timeStampStr) / 10 : Long.parseLong(timeStampStr);
                date = new Date(timeStamp);
            }
            SimpleDateFormat sdf2 = new SimpleDateFormat(format);
            return sdf2.format(date);
        } catch (ParseException e) {
            timeStampStr = null;
            e.printStackTrace();
        }
        return null;
    }
}
